<?php
$nb_max="20"; // number of video per page

$page['video']=array();
$page['video_first_page']="";
$page['video_previous_page']="";
$page['video_last_page']="";
$page['video_next_page']="";
$page['L_message']="";

# suppression
if($right_user['delete_video'] AND isset($_GET['v2']) AND $_GET['v2']=="delete" AND isset($_GET['v3']) AND !empty($_GET['v3'])) 
{
 $var['id']=$_GET['v3'];
 $sql_sup=sql_replace($sql['video']['sup_video'],$var);
 $sgbd = sql_connect();

 if(sql_query($sql_sup) != false) { $page['L_message']=$lang['video']['form_video_sup_1']; }
 else { $page['L_message']=$lang['video']['form_video_sup_0']; }

 sql_close($sgbd);
}


# TRI
# $_GET['v1'] is a variable like : page_1_name_asc
if(isset($_GET['v1']) AND stristr($_GET['v1'],"page"))
{
 $v=explode("_",$_GET['v1']);
 $page_num=$v['1'];
 $tri=$v['2'];
 $ordre=$v['3'];
}
else
{
 $page_num=1; // number of the page
 $tri="name"; // tri par defaut
 $ordre="asc"; // ordre par defaut
}

# ORDRE (sens)
if($ordre=="desc") { $sens="desc"; $sens_inv="asc"; }
else { $sens="asc"; $sens_inv="desc"; }


/************************/
/* START CONDITIONS  */
/************************/
if(!isset($var['condition']) OR $var['condition']=="")
{
 $condition=array();

 # creation of conditions list
 $nb_condition=sizeof($condition);
 if($nb_condition==0) { $var['condition']=""; }
 elseif($nb_condition=="1") { $var['condition']="WHERE ".$condition['0']; }
 else { $var['condition']="WHERE ".implode(" AND ",$condition); }
}
/**********************/
/* END OF CONDITIONS    */
/**********************/


/**********************/
/* ORDER (tri) */
/**********************/
if(!isset($var['order']))
{
 switch($tri) {
  case "name" : $var['order']=" ORDER BY video_name ".$sens." "; break;
  default : $var['order']=" ORDER BY video_name ".$sens." ";
 }
}

$page['page']=array();
if(!isset($var['limit']))
{
 /* we get the number of video */
 $sql_nb=sql_replace($sql['video']['select_video_nb'],$var);
 $sgbd = sql_connect();
 $res_nb = sql_query($sql_nb);
 $ligne=sql_fetch_array($res_nb);
 $nb=$ligne['nb'];
 sql_free_result($res_nb);
 sql_close($sgbd);


 /***************/
 /* PAGINATION */
 /**************/
 # number of the current page
 $var['limit']="LIMIT ".($page_num-1)*$nb_max.",".$nb_max;
 $nb_page=ceil($nb/$nb_max);

 $url="index.php?r=".$plugin_idurl."&v1=page_";
 $end_url="_".$tri."_".$sens;

 $page['page']=generate_pagination($url, $nb_page,$page_num,$end_url);

 # previous page (except on the first one)
 if($page_num!=1)
 {
  $page['video_first_page']=convert_url($url."1".$end_url);
  $page['video_previous_page']=convert_url($url.($page_num - 1).$end_url);
 }
 # next page (except on the last one)
 if($page_num!=$nb_page)
 {
  $page['video_last_page']=convert_url($url.$nb_page.$end_url);
  $page['video_next_page']=convert_url($url.($page_num + 1).$end_url);
 }
 /******************/
 /* END PAGINATION */
 /******************/

}


$sql_video = sql_replace ($sql['video']['select_video_condition'], $var) ;

// echo $sql_video ;

$sgbd = sql_connect();
$res_video = sql_query($sql_video);
$nb_ligne=sql_num_rows($res_video);

// echo '----------' . $nb_ligne ;

if(!$right_user['video_list']) {
	$page['L_message_club']=$lang['general']['acces_reserve_admin'];
}
elseif($nb_ligne=="0")
{
 $page['L_message']=$lang['video']['E_video_not_found'];
}
else
{
 $i="0";
 while($ligne = sql_fetch_array($res_video))
 {
    $page['video'][$i]['id']=$ligne['video_id'];
    $page['video'][$i]['name']=$ligne['video_name'];
    $page['video'][$i]['date']=$ligne['video_date'];
    $page['video'][$i]['url']=$ligne['video_url'];

    $toUrlVideos = getVideo ($ligne['video_url']) ;
    if ($toUrlVideos)
    {
        $page['video'][$i]['url_thumb'] = $toUrlVideos['thumb'] ;
        $page['video'][$i]['url_video'] = $toUrlVideos['embed'] ;
    }
    else
    {
        $page['video'][$i]['url_thumb'] = '' ;
        $page['video'][$i]['url_video'] = '' ;
    }

    $page['video'][$i]['description']=$ligne['video_description'];  
    $page['video'][$i]['status']=$ligne['video_status'];  
    $page['video'][$i]['mod']=$i%2;

    $page['video'][$i]['L_show_view']=$lang['video']['show_view'];
    $page['video'][$i]['video_view']=convert_url("index.php?r=".$plugin_idurl."&v1=view&v2=".$ligne['video_id']);

    if(isset($var['value_video']) AND $var['value_video']==$ligne['video_id']) { $page['video'][$i]['selected']="selected"; } else { $page['video'][$i]['selected']=""; }

    $page['video'][$i]['video_edit']="";
    $page['video'][$i]['video_delete']="";
    if($right_user['edit_video'])
    {
        $page['video'][$i]['video_edit']=convert_url("index.php?r=".$plugin_idurl."&v1=form_video&v2=".$ligne['video_id']); 
    }
    if($right_user['delete_video'])
    {
        $page['video'][$i]['video_delete']=convert_url("index.php?r=".$plugin_idurl."&v1=video_list&v2=delete&v3=".$ligne['video_id']);
    }
    $i++;
 }
}

sql_free_result($res_video);
sql_close($sgbd);

if($right_user['add_video'])
 {
  $page['video_add']=convert_url("index.php?r=".$plugin_idurl."&v1=form_video");
  $page['admin']="1";
 }
else
{
 $page['video_add']="";
 $page['admin']="";
}

# text
$page['link_video']=convert_url("index.php?r=".$plugin_idurl."&v1=video_list");
$page['video_tri_name']=convert_url("index.php?r=".$plugin_idurl."&v1=page_".$page_num."_name_".$sens_inv);
$page['video_tri_city']=convert_url("index.php?r=".$plugin_idurl."&v1=page_".$page_num."_city_".$sens_inv);
$page['video_tri_place']=convert_url("index.php?r=".$plugin_idurl."&v1=page_".$page_num."_place_".$sens_inv);


$page['L_order']=$lang['video']['order_by'];
$page['L_name']=$lang['video']['name'];
$page['L_date']=$lang['video']['date'];

$page['L_first_page']=$lang['video']['first_page'];
$page['L_previous_page']=$lang['video']['previous_page'];
$page['L_next_page']=$lang['video']['next_page'];
$page['L_last_page']=$lang['video']['last_page'];

$page['L_add']=$lang['video']['add_video'];
$page['L_title']=$lang['video']['video_list'];

# meta
$page['meta_title']=$lang['video']['video_list'];
$page['template']=$tpl['video']['video_list'];

?>